﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;

namespace EAGL.Core
{
    /// <summary>
    /// A Utility class that have many useful conversion operations
    /// </summary>
    public static class GeneralConvertor
    {
        public static IField GetField(string name, List<IField> list)
        {
            IField found = null;
            foreach (IField f in list)
            {
                if (f.Name.Trim().ToUpper() == name.Trim().ToUpper())
                {
                    found = f;
                    break;
                }
            }
            return found;
        }
        
        public static List<IField> ListOf(List<String> wantedFields, List<IField> allFields)
        {
            List<IField> fieldsList = new List<IField>();

            foreach (string s in wantedFields)
            {
                IField f = GetField(s, allFields);
                if (f != null)
                {
                    fieldsList.Add(f);
                }
            }
            return fieldsList;
        }


        public static List<string> ListOf(string s, bool trim)
        {
            string[] l = s.Split(',');
            List<string> list = l.ToList<string>();
            int c = list.Count;
            if (trim)
            {
                for (int i = 0; i < c; i++)
                {
                    list[i] = list[i].Trim();
                }
            }
            return list;
        }

        public static List<string> ListOf(List<IField> fields)
        {
            List<string> l = new List<string>();
            int c = fields.Count;
            int i = 0;
            while (i < c)
            {
                l.Add(fields[i].Name);
                i++;
            }
            return l;
        }
        

        public static List<IField> ListOf(IFields fields)
        {
            List<IField> fieldsList = new List<IField>();
            int c = fields.FieldCount;
            for (int i = 0; i < c; i++)
            {
                fieldsList.Add(fields.get_Field(i));
            }
            return fieldsList;
        }

        public static IFields ToFields(List<IField> list)
        {
            IFields fields = new FieldsClass();
            IFieldsEdit fe = fields as IFieldsEdit;

            foreach (IField f in list)
            {
                fe.AddField(f);
            }
            return fields;
        }

        
        public static string StringOf(List<string> fields)
        {
            StringBuilder sb = new StringBuilder();
            int i = 0;
            int c = fields.Count;
            while (i < c)
            {
                sb.Append(fields[i]);
                if (i < c-1)
                    sb.Append(", ");
                i++;
            }
            return sb.ToString();
        }
 

    }
}
